package com.vini.web.panels;

import java.util.Arrays;

import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.AjaxLink;
import org.apache.wicket.ajax.markup.html.form.AjaxSubmitLink;
import org.apache.wicket.extensions.ajax.markup.html.modal.ModalWindow;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.ListMultipleChoice;
import org.apache.wicket.markup.html.form.RadioChoice;
import org.apache.wicket.markup.html.form.RequiredTextField;
import org.apache.wicket.markup.html.form.TextArea;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.Model;
import org.apache.wicket.spring.injection.annot.SpringBean;
import com.vini.core.ec.Company;
import com.vini.core.si.CompanyService;
import com.vini.web.pages.SignUp;

public class CompanyPanel extends Panel {

	private static final long serialVersionUID = 1L;

	private Form<Company> compForm;
	private RequiredTextField<String> compName;
	private RequiredTextField<String> addressLine1;
	private RequiredTextField<String> addressLine2;
	private RequiredTextField<String> city;
	private RequiredTextField<String> state;
	private RadioChoice<String> roleCompanyTaxi;
	private RequiredTextField<String> sector;
	//private RequiredTextField<String> mobileNo;
	private TextArea<String> areas;
	private ListMultipleChoice<String> branches;
	private RequiredTextField<String> rate;
	private RequiredTextField<Integer> employees;
	private AjaxSubmitLink register;
	private AjaxLink<String> addVehicles;
	private ModalWindow vehicleWindow;
	private ModalWindow modalWindow;
	private CompoundPropertyModel<Company> companyModel;

	@SpringBean
	private CompanyService companyService;

	public CompanyPanel(String id) {
		super(id);
		createVehicleWindow();
		initModalWindow();
		
		companyModel = new CompoundPropertyModel<Company>(new Company());
		compForm = new Form<Company>("form", companyModel){
			private static final long serialVersionUID = 1L;
			
			@Override
			protected void onSubmit() {
				Company company = compForm.getModelObject();
				String address = addressLine1.getModelObject()+", "+addressLine2+", "+
				city.getModelObject()+", "+state.getModelObject();
				company.setAddress(address);
				company = companyService.save(company);
				if (company.getPCompanyId() != null) {
					info(company.getName()+" Registered Successfully.");
				}
			}
			@Override
			protected void onError() {
				// TODO Auto-generated method stub
				super.onError();
			}
		};
		
		compName = new RequiredTextField<String>("name");
		addressLine1 = new RequiredTextField<String>("line1", Model.of(""));
		addressLine2 = new RequiredTextField<String>("line2", Model.of(""));
		city = new RequiredTextField<String>("city", Model.of(""));
		state = new RequiredTextField<String>("state", Model.of(""));
		roleCompanyTaxi = new RadioChoice<String>("roleTaxiCompany", Arrays.asList(new String[]{"Company","Taxi"}));
		sector = new RequiredTextField<String>("sector");
		//mobileNo = new RequiredTextField<String>("mobileNo");
		areas = new TextArea<String>("areas");
		branches = new ListMultipleChoice<String>("branches");
		rate = new RequiredTextField<String>("rate");
		employees = new RequiredTextField<Integer>("employeeNumber");
		
		addVehicles = new AjaxLink<String>("addVehicle") {

			private static final long serialVersionUID = 1L;

			@Override
			public void onClick(AjaxRequestTarget target) {
				target.appendJavaScript("Wicket.Window.unloadConfirmation = false;");
				vehicleWindow.show(target);
			}
		};
		register = new AjaxSubmitLink("register") {
			private static final long serialVersionUID = 1L;

			@Override
			public void onSubmit(AjaxRequestTarget target, Form<?> form) {
				target.appendJavaScript("Wicket.Window.unloadConfirmation = false;");
				modalWindow.show(target);
			}
			@Override
			protected void onError(AjaxRequestTarget target, Form<?> form) {
				error("Error");
				modalWindow.show(target);
				super.onError(target, form);
			}
		};

		compForm.add(compName);
		compForm.add(addressLine1);
		compForm.add(addressLine2);
		compForm.add(city);
		compForm.add(state);
		compForm.add(roleCompanyTaxi);
		compForm.add(sector);
		//compForm.add(mobileNo);
		compForm.add(areas);
		compForm.add(branches);
		compForm.add(rate);
		compForm.add(employees);
		compForm.add(addVehicles);
		compForm.add(register);
		add(compForm);
		add(vehicleWindow);
		add(modalWindow);
	}

	private ModalWindow createVehicleWindow() {
		vehicleWindow = new ModalWindow("vehicle");
		vehicleWindow.setTitle(Model.of("Upload Vehicles"));
		vehicleWindow.setOutputMarkupId(true);
		vehicleWindow.setInitialHeight(200);
		vehicleWindow.setInitialWidth(500);
		VehiclePanel vehiclePanel = new VehiclePanel(vehicleWindow.getContentId());
		vehicleWindow.setContent(vehiclePanel);
		
		vehicleWindow.setCloseButtonCallback(new ModalWindow.CloseButtonCallback() {  
            private static final long serialVersionUID = 1L;

			public boolean onCloseButtonClicked(AjaxRequestTarget target) {  
				setResponsePage(SignUp.class);
                return true;
            }  
        });  
		
		return vehicleWindow;
	}
	
	private void initModalWindow() {
		modalWindow = new ModalWindow("modalWindow");
		modalWindow.setTitle(Model.of("Message"));
		modalWindow.setInitialHeight(100);
		modalWindow.setInitialWidth(600);
		FeedbackPanel panel = new FeedbackPanel(modalWindow.getContentId());
		modalWindow.setContent(panel);
		//modalWindow.setCssClassName("modal.css")
	}

}
